home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: [] overloding vc4.0
- Date: 24 Jan 1996 18:53:04 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan24135304@g7240065.bridge.bst.bls.com>
- References: <NEWTNews.822530713.12255.moti@motisaad.netmanage.co.il>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: Moti Saadon's message of Wed, 24 Jan 96 16:41:53 PDT
-
- In article <NEWTNews.822530713.12255.moti@motisaad.netmanage.co.il> Moti Saadon <moti@netmanage.co.il> writes:
-
- : Why can't I compile this code at vc++4.0?
- : Thanks.
-
- Posting the error would help identify the problem.
- Anyway, my comments are on lines starting with 'X'
-
- Hope it helps
- Regards
-
- -A.
-
- //----------------------------begin code-----------------
-
- : //temp.cpp
-
- : #include <stdio.h>
- X #include <stdlib.h> // For EXIT_SUCCESS return from main.
-
- : #include <iostream.h>
- X This include is not needed as you are using printf
-
- : class Tclass{
- : public:
- : int temp;
- : };
-
- : //Range exeption
- : class Range {
- : int low, hi, badIndex;
- : public:
- : Range (int l, int h, int b)
- : { low = l; hi = h; badIndex = b; }
-
- : void display()
- : { printf("\nRange error {%d, %d} :%d",
- : low, hi, badIndex); }
- X Why not use the stream "cout" ??
-
- : };
-
-
- : template <class Type>
- : class Array {
- : Type *array;
- : int size; //size of the array
- : public:
- : Array (int sz)
- : { array = new Type[size = sz];}
- : ~Array () {delete [] array;}
- : Type &operator [] (int i);
- : };
-
- : //now comes the definition of the subscript operator
- : template <class Type>
- : Type &Array::operator[] (int i)
- X You are probably getting some complaint here like: class Array must be
- X qualified with parameter list of instantiations or maybe not, but this
- X should be:
- X Type &Array<Type>::operator[] (int i)
-
- : {
- : if (i<0 || i>=size)
- : throw Range(0, size-1, i);
- : return array[i];
- : }
-
- : void func(Array<Tclass> &arr)
- : {
- : try {
- : for (int i=0; i<5; i++)
- : //do some work with arr[i]
- : arr[i].temp = 0;
-
- : }
- : catch (Range &r) {
- : // report error condition
- : r.display();
- : }
- : }
-
-
- : void main()
- X This should be
- X int main(void)
-
- : {
- : //define Array of size 50
- X This comment lies and is not reflected in the declaration below.
- : Array<Tclass> array(3);
- X Obviously you have made this value too small to demonstrate the exception.
-
- : func(array);
-
- X Should probably return something here now that main is declared properly
- X return EXIT_SUCCESS;
- : }
-
- //------------------------end of code ---------------------
-
- --
- | A.Champion |
-